package com.yanqu.road.server.cmd.activity.gourdchild;


import com.yanqu.road.entity.activity.gourdchild.GourdChildConfig;
import com.yanqu.road.entity.activity.gourdchild.GourdChildUserData;
import com.yanqu.road.pb.activity.GourdChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.gourdchild.GourdChildModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.gourdchild.GourdChildMgr;
import com.yanqu.road.server.manger.activity.gourdchild.MoveMergerResult;
import com.yanqu.road.server.pb.GourdChildPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_GOURD_CHILD_MOVE_GRID, desc = "移动")
public class GourdChildMoveGridCmd extends AbstractCommand {

    int direction;

    private GourdChildUserData userData = null;

    MoveMergerResult moveMergerResult = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GourdChildProto.GourdChildMoveGridReqMsg reqMsg = GourdChildProto.GourdChildMoveGridReqMsg.parseFrom(packet.getBytes());
        direction = reqMsg.getDirection();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        GourdChildConfig config = GourdChildMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_OPEN;
            return;
        }

        GourdChildModule module = player.getModule(GourdChildModule.class);
        userData = module.getUserData2();
        if (userData == null) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_UNLOCK;
            return;
        }

        moveMergerResult = module.moveGrid(direction);

        ret = moveMergerResult.getRet();
    }

    @Override
    public void sendData(GamePlayer player) {
        GourdChildProto.GourdChildMoveGridRespMsg.Builder respMsg = GourdChildProto.GourdChildMoveGridRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(ret == 0){
            GourdChildProto.GourdChildGameDataMsg.Builder gameData = GourdChildPb.parseGameDataMsg(userData, GourdChildMgr.getConfig());
            GourdChildMgr.appendMovableData(gameData, userData.getGridList(), GourdChildMgr.getConfig());
            respMsg.setGameData(gameData);
            if(moveMergerResult != null){
                respMsg.setEnd(moveMergerResult.isEnd());
                respMsg.setWin(moveMergerResult.isWin());
                respMsg.setRound(moveMergerResult.getRound());
                respMsg.setReward(moveMergerResult.getReward());
                respMsg.addAllAttack(GourdChildPb.getGourdChildGridTemps(moveMergerResult.getAttackList()));
                respMsg.addAllDamage(moveMergerResult.getAttackDamage());
                respMsg.setHighDamage(moveMergerResult.getHighDamage());
                respMsg.setNewHigh(moveMergerResult.isNewHigh());
                respMsg.setStar(moveMergerResult.getStar());
            }
        }
        player.sendPacket(Protocol.U_GOURD_CHILD_MOVE_GRID, respMsg);
    }


}
